package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.domain.Role;
import cn.wolfcode.education.mapper.DeptMapper;
import cn.wolfcode.education.mapper.EmployeeMapper;
import cn.wolfcode.education.query.EmployeeQueryObject;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.IEmployeeService;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DeptMapper deptMapper;

    public PageResult list(QueryObject qo) {
        Integer totals = employeeMapper.queryForCount(qo);
        if (totals == 0) {
            return new PageResult();
        }
        List list = employeeMapper.queryForList(qo);
        return new PageResult(totals, list);
    }

    public void delete(Long id) {

        employeeMapper.updateStateById(id, Employee.STATE_LOCK);
    }



    public void save(Employee employee) {
        employee.setInputTime(new Date());

        employee.setPassword(new Md5Hash("000000", employee.getUsername()).toString());
        employeeMapper.insert(employee);
        for (Role role : employee.getRoles()) {
            employeeMapper.insertRelationWithRole(employee.getId(), role.getId());
        }
    }


//    @Test
//    public void test(){
//        System.out.println(new Md5Hash("000000","admin",1));
//    }


    public void edit(Employee employee) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelationWithRole(employee.getId());
        for (Role role : employee.getRoles()) {
            employeeMapper.insertRelationWithRole(employee.getId(), role.getId());
        }
    }

    public List queryForList(EmployeeQueryObject qo) {
        return employeeMapper.queryForList(qo);
    }

    public List<Employee> queryAll() {
        return employeeMapper.queryAll();
    }


    public Employee retrieveSelectEmployee(String username) {
        return employeeMapper.retrieveSelectEmployee(username);
    }

    //查询登陆用户
    public Employee getByUsername(String username) {
        return employeeMapper.getByUsername(username);
    }

    public List<Employee> queryEmpName() {
        return employeeMapper.queryEmpName(1L);
    }

    /**
     * 根据当前用户获取其所属部门下的员工
     *
     * @return
     */
    public List<Employee> selectEmployeeByDeptId() {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        List<Employee> list;
        //判断是否是超级管理员
        if (currentUser.isAdmin()) {
            //获取所有部门管理员
            list = employeeMapper.selectAllManager();
        } else {
            List<Long> deptIds = deptMapper.getDeptIdsByManagerId(currentUser.getId());
            //查找员工列表
            list = employeeMapper.selectEmployeeByDeptId(deptIds);
        }
        return list;
    }

    public List<Employee> allSellEmp() {
        return employeeMapper.allSellEmp();
    }

    /**
     * 忘记密码后修改密码
     *
     * @param newpassword
     * @param username
     */

    public void updatePassword(String newpassword, String username) {

        String enPass = new Md5Hash(newpassword, username).toString();
        System.out.println(enPass);
        employeeMapper.updatePassword(enPass, username);
    }

    /**
     * 登陆后修改密码
     *
     * @param username
     * @param password
     */

    public void changePassword(String username, String password) {

        String enPass = new Md5Hash(password, username).toString();
        employeeMapper.updatePassword(enPass, username);
    }

    public List<Employee> selectTeacherByRoleIdWithSchedle(Long id) {
        return employeeMapper.selectTeacherByRoleIdWithSchedle(id);
    }
}
